package com.dataseek.iot.toolikit.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

//对象工具类
public class CustomBeanUtils {
    //将Map对象列表转换为自定义对象列表
    public static <T> List<T> mapListToBeanList(List<? extends Map> mapList,Class<T> beanClass){
        List<T> list = null;
        String mapListStr = JSON.toJSONString(mapList);
        List<T> tList = JSONArray.parseArray(mapListStr, beanClass);
        list=tList;
        return list;
    }

    //将Map对象转为自定义对象
    public static <T> T mapToBean(Map map,Class<T> beanClass){
        JSONObject mapObject = new JSONObject(map);
        T t = JSON.parseObject(mapObject.toJSONString(),beanClass);
        return t;
    }

    //判断字符串是否为数字
    public static boolean isNumericZidai(String str) {
        for (int i = 0; i < str.length(); i++) {
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    //将任意对象列表转换为自定义对象列表(字段匹配度)
    public static <T> List<T> anyListToBeanList(List anyList,Class<T> beanClass){
        List<T> list = null;
        String listStr = JSON.toJSONString(anyList);
        List<T> tList = JSONArray.parseArray(listStr, beanClass);
        list=tList;
        return list;
    }

    //根据指定key进行单级排序
    public static JSONArray jsonArraySort(JSONArray jsonArr,String sortKey,boolean is_desc) {
        JSONArray sortedJsonArray = new JSONArray();
        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.getJSONObject(i));
        }
        Collections.sort(jsonValues, new Comparator<JSONObject>() {
            private  final String KEY_NAME = sortKey;

            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valA = new String();
                String valB = new String();

                try {
                    valA = a.getString(KEY_NAME);
                    valB = b.getString(KEY_NAME);
                    if(isNumericZidai(valA)&&isNumericZidai(valB)){
                        Integer intvalA = Integer.parseInt(valA);
                        Integer intvalB = Integer.parseInt(valB);
                        if (is_desc){
                            return -intvalA.compareTo(intvalB);
                        } else {
                            return -intvalB.compareTo(intvalA);
                        }
                    }
                    else{
                        if (is_desc){
                            return -valA.compareTo(valB);
                        } else {
                            return -valB.compareTo(valA);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
        for (int i = 0; i < jsonArr.size(); i++) {
            sortedJsonArray.add(jsonValues.get(i));
        }
        return sortedJsonArray;
    }

    //多级父子JsonArray排序
    public static JSONArray allLevelSort(JSONArray jsonArray,String sortKey,String childrenKey,boolean is_desc){
        JSONArray sortedArray = null;
        sortedArray = jsonArraySort(jsonArray,sortKey,is_desc);
        for(Object obj:sortedArray){
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray childrenArray = jsonObject.getJSONArray(childrenKey);
            if(childrenArray!=null&&childrenArray.size()>0)
            {
                JSONArray subSortedArray = allLevelSort(childrenArray,sortKey,childrenKey,is_desc);
                jsonObject.put(childrenKey,subSortedArray);
            }
        }
        return sortedArray;
    }
}
